package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorsMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.aspectj.bridge.Message;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private FlavorsMapper flavorsMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @Transactional//事务处理
    @Override
    public void savaWithFlavors(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //向菜品表里面加入一条数据
        dishMapper.sava(dish);

        //获取insert语句生成的主键值
        Long id = dish.getId();

        //向口味表里面加数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            flavorsMapper.insertBatch(flavors);
        }


    }

    /**
     * 菜品分页查询
     */
    @Override
    public PageResult dishPage(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<Dish> page = dishMapper.dishPage(dto);
        Long total = page.getTotal();
        List<Dish> result = page.getResult();
        return new PageResult(total,result);
    }

    /**
     * 菜品批量删除
     */
    @Override
    public void dishDelete(List<Long> ids) {
//        判断当前菜品是否可以删除--是否存在起售的菜品
        for (Long id : ids){
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
//        判断是否可以删除--是否存在关联的套餐
        List<Long> bySetId = setmealDishMapper.getBySetId(ids);
        if (bySetId != null && bySetId.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
//        删除菜品
        for(Long id : ids){
            dishMapper.dishDelete(id);
        }
//        删除口味
        for(Long id : ids){
            flavorsMapper.flavorsDelete(id);
        }
    }

    /**
     * 菜品起售或停止
     */
    @Override
    public void startOrStop(String status, String id) {
        dishMapper.startOrStop(status,id);
    }

    /**
     * 修改菜品信息
     */
    //根据ID回显数据
    @Override
    public DishDTO getById(String id) {
        DishDTO dishDTO = dishMapper.getById1(id);
        return dishDTO;
    }

    @Transactional
    @Override
    public void dishUpdate(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //修改基本信息
        dishMapper.dishUpdate(dish);
//        先删除原先口味
        flavorsMapper.flavorsDelete(dish.getId());
//        再插入现在口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            flavorsMapper.insertBatch(flavors);
        }
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorsMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }







    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
}
